//
//  NewsViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/1/16.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "NewsViewController.h"
#import "RHTableView.h"
#import "NewsTableViewCell.h"
#import "InsuranceBrokersContentViewController.h"
#import "NavigationBarToggleTabView.h"

@interface NewsViewController (){
    RHTableView *tabelViewC;
    NavigationBarToggleTabView *toggleTab;
    NSArray *arrayTypes;
    NSString *strType;
}

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    RegisterNotify(@"againLoadUserData", @selector(againLoadUserData))
    
    [self navbarTitle:@" "];
    [self initNavTopButtons];
    
    tabelViewC=[[RHTableView alloc]initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, kContentHeight)];
    [tabelViewC showRefresh:YES LoadMore:YES];
    tabelViewC.delegate = self;
    tabelViewC.dataSource = self;
    //    tabelViewC.delegate2=self;
    [tabelViewC setBackgroundColor:rgbGray];
    [tabelViewC setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [tabelViewC registerClass:[NewsTableViewCell class] forCellReuseIdentifier:@"NewsTableViewCell"];
    [tabelViewC setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    
    [self.view addSubview:tabelViewC];
    
    
    [self againLoadUserData];
}
-(void)initNavTopButtons{
    toggleTab=[[NavigationBarToggleTabView alloc] initWithFrame:CGRectMake((kScreenWidth-225)/2, 28, 225, 28)];
    [self.navView addSubview:toggleTab];
    
}
-(void)refreshNavTopButtonsData{
    toggleTab.hidden=NO;
    __weak NewsViewController *viewC=self;
    [toggleTab setTiltelArray:arrayTypes toggleTab:^(NSDictionary *dicSelect) {
        strType=[dicSelect valueForJSONStrKey:@"id"];
        [viewC refreshTableData];
        if ([[dicSelect valueForJSONStrKey:@"id"] isEqualToString:@"1"]) {
            
        }else{
            
        }
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self refreshTableData];
}


#pragma mark selector
-(void)againLoadUserData{
    [self loadNewTypeData];
    
}
-(void)loadNewTypeData{
//    XKFV3newsnewscate
    krequestParam
    [NetEngine createGetAction_LJ_two:[NSString stringWithFormat:@"%@%@",XKFV3newsnewscate,dictparam.wgetParamStr] onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"]isEqualToString:@"200"]) {
            arrayTypes=[[resData objectForJSONKey:@"data"] objectForJSONKey:@"cate"];
            if ([[Utility Share] userIsADD] || [arrayTypes count]<=1) {
                [self navbarTitle:@"资讯"];
                toggleTab.hidden=YES;
            }else{
                [self navbarTitle:@" "];
                [self refreshNavTopButtonsData];
            }
        }else{
           [self loadNewTypeData];
        }
    } onError:^(NSError *error) {
        [self loadNewTypeData];
    }];
}
-(void)refreshTableData{
    tabelViewC.urlString=[NSString stringWithFormat:XKFnews,[[Utility Share] userId],[[Utility Share] userToken],strType?strType:@"",@"",@"%@"];
    [tabelViewC refresh];
}

#pragma mark tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tabelViewC.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 95;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=[tabelViewC.dataArray objectAtIndex:indexPath.row];
    NewsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"NewsTableViewCell" forIndexPath:indexPath];
    [cell setValueForDictionary:dic indexPath:indexPath type:@"news"];
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
      NSDictionary *dic=[tabelViewC.dataArray objectAtIndex:indexPath.row];
    [self pushController:[InsuranceBrokersContentViewController class] withInfo:@"new" withTitle:[self.otherInfo valueForJSONStrKey:@"subTitle"] withOther:dic];
    if ([[dic valueForJSONStrKey:@"isred"] isEqualToString:@"0"]) {
        NSMutableDictionary *dicTemp=[NSMutableDictionary dictionaryWithDictionary:dic];
        [dicTemp setValue:@"1" forKey:@"isred"];
        [tabelViewC.dataArray replaceObjectAtIndex:indexPath.row withObject:dicTemp];
        [tabelViewC reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
